/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "ProbModel.h"

Evolutive::CProbModel::CProbModel(void):m_VarRangeMin(NULL),m_VarRangeMax(NULL),m_EstimationWorsePer(0.0)
{	
}

Evolutive::CProbModel::~CProbModel(void)
{
	// Release the remaining memory
	if(m_VarRangeMin)
		delete[] m_VarRangeMin;
	if(m_VarRangeMax)
		delete[] m_VarRangeMax;
}

void Evolutive::CProbModel::InitializeModel(int NumVars,Evolutive::CPopulation &Population)
{
	// Store the problem description
	m_NumVars=NumVars;

	// Release the old ranges memory
	if(m_VarRangeMin)
		delete[] m_VarRangeMin;
	if(m_VarRangeMax)
		delete[] m_VarRangeMax;	

	// Allocate new memory
	m_VarRangeMin=new int[NumVars];
	m_VarRangeMax=new int[NumVars];
}

Evolutive::CProbModel& operator<<(Evolutive::CProbModel &ProbModel, Evolutive::CChromosome C)
{
	// Update the model using the new individual
	ProbModel.Update(C);

	// Return the modified model
	return ProbModel;
}

void Evolutive::CProbModel::NewPopulation(CPopulation &Population,CODING_METHOD ModelCode)
{
	// Generate a new population without evaluation function. This is a call to a pure virtual function.
	NewPopulation(Population,ModelCode,NULL);
}

void Evolutive::CProbModel::SetRange(int VarIdx,int MinVal,int MaxVal)
{
	// Check the index value
	if(VarIdx<0 || VarIdx>m_NumVars)
		throw CEvolutiveLibException("Index out of bounds",__FILE__,__LINE__,"SetRange");

	// Verify the range list
	if(!m_VarRangeMin || !m_VarRangeMax)
		throw CEvolutiveLibException("NULL range list",__FILE__,__LINE__,"SetRange");

	// Store the values
	m_VarRangeMin[VarIdx]=MinVal;
	m_VarRangeMax[VarIdx]=MaxVal;
}

void Evolutive::CProbModel::SetEqualRanges(int MinVal,int MaxVal)
{
	register int i;

	// Assign these values to all the variables
	for(i=0;i<m_NumVars;i++)
	{
		SetRange(i,MinVal,MaxVal);
	}
}

void Evolutive::CProbModel::SetEstimationWorseElements(double Percentage)
{
	m_EstimationWorsePer=Percentage;
}
